from urllib import request # 导入urllib库中的request模块
from urllib import parse
from fake_useragent import UserAgent
import webbrowser

ua = UserAgent()

#请求头创建
headers = {
    'User-Agent': ua.random,
}
#参数拼接和解析
url = 'https://www.google.com/search?'+parse.urlencode({'q':'油管'})

# 发送请求
req = request.Request(url, headers=headers)
# 接收响应
response = request.urlopen(req)
# 读取响应内容
html = response.read().decode('utf-8')
with open('baidu.html', 'w', encoding='utf-8') as f:
    f.write(html)
# 打开浏览器显示结果
webbrowser.open('baidu.html')
